package com.xjgc.msibase.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.msibase.entity.MsiServer;
import com.xjgc.msibase.entity.MsiServerTask;
import com.xjgc.msibase.service.IMsiServerService;
import com.xjgc.msibase.service.IMsiServerTaskService;
import com.xjgc.msibase.vo.MsiServerPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 中间库WebService服务
 * @Author: 小逗号
 * @Date: 2023-11-02
 * @Version: V1.0
 */
@Api(tags = "中间库WebService服务")
@RestController
@RequestMapping("/msibase/msiServer")
@Slf4j
public class MsiWsServerController extends JeecgController<MsiServer, IMsiServerService> {
    @Resource
    private IMsiServerService msiServerService;
    @Resource
    private IMsiServerTaskService msiServerTaskService;

    /**
     * 分页列表查询
     *
     * @param msiWsServer
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoDict
    //@AutoLog(value = "中间库WebService服务-分页列表查询")
    @ApiOperation(value = "中间库WebService服务-分页列表查询", notes = "中间库WebService服务-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MsiServer>> queryPageList(MsiServer msiWsServer,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<MsiServer> queryWrapper = QueryGenerator.initQueryWrapper(msiWsServer, req.getParameterMap());
        Page<MsiServer> page = new Page<MsiServer>(pageNo, pageSize);
        IPage<MsiServer> pageList = msiServerService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param msiServerPage
     * @return
     */
    @AutoLog(value = "msi_server-添加")
    @ApiOperation(value="msi_server-添加", notes="msi_server-添加")
    //@RequiresPermissions("com.xjgc:msi_server:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody MsiServerPage msiServerPage) {
        MsiServer msiServer = new MsiServer();
        BeanUtils.copyProperties(msiServerPage, msiServer);
        msiServerService.saveMain(msiServer, msiServerPage.getMsiServerTaskList());
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param msiServerPage
     * @return
     */
    @AutoLog(value = "msi_server-编辑")
    @ApiOperation(value="msi_server-编辑", notes="msi_server-编辑")
    //@RequiresPermissions("com.xjgc:msi_server:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody MsiServerPage msiServerPage) {
        MsiServer msiServer = new MsiServer();
        BeanUtils.copyProperties(msiServerPage, msiServer);
        MsiServer msiServerEntity = msiServerService.getById(msiServer.getId());
        if(msiServerEntity==null) {
            return Result.error("未找到对应数据");
        }
        //msiServerService.updateMain(msiServer, msiServerPage.getMsiServerTaskList());
        msiServerService.updateById(msiServer);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "msi_server_task通过主表ID查询")
    @ApiOperation(value="msi_server_task主表ID查询", notes="msi_server_task-通主表ID查询")
    @GetMapping(value = "/queryMsiServerTaskByMainId")
    public Result<List<MsiServerTask>> queryMsiServerTaskListByMainId(@RequestParam(name="id",required=true) String id) {
        List<MsiServerTask> msiServerTaskList = msiServerTaskService.selectByMainId(id);
        return Result.OK(msiServerTaskList);
    }

}
